package com.jinzhi.jzweb.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.jzweb.domain.JazGroupProdDO;
import com.jinzhi.jzweb.domain.JazGroupSpecsDO;
import com.jinzhi.jzweb.domain.JazSignupUserDO;
import com.jinzhi.jzweb.domain.JazUserOrderDO;
import com.jinzhi.jzweb.domain.param.UserExamineStatus;
import com.jinzhi.jzweb.service.JazSignupUserService;
import jodd.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 报名管理页面
 */
@Controller
@RequestMapping("/signup")
public class SignUpController {
    @Autowired
    private JazSignupUserService jazSignupUserService;
    @GetMapping("/list")
    @RequiresPermissions("signup:list:list")
    String index(){
        return "jzweb/signUp/index";
    }
    @GetMapping("/info/{id}/{type}")
    String infor( @PathVariable("id") Long id,@PathVariable("type") String type,  Model model) {
        model.addAttribute("id", id);
        model.addAttribute("type", type);
        JazSignupUserDO myData=jazSignupUserService.getById(id);
        if(null==myData){
            myData=new JazSignupUserDO();
        }
        model.addAttribute("myData",myData);
        model.addAttribute("userImgs",!StringUtil.isNotEmpty(myData.getUserImg())?new String[0]:myData.getUserImg().split(","));
        model.addAttribute("others",!StringUtil.isNotEmpty(myData.getOther())?new String[0]:myData.getOther().split(","));
        model.addAttribute("identityCardImgs",!StringUtil.isNotEmpty(myData.getIdentityCardImg())?new String[0]:myData.getIdentityCardImg().split(","));

        List<UserExamineStatus> lists=new ArrayList<>();
        if(StringUtil.isNotEmpty(myData.getHistoryReview())){
            lists=JSON.parseArray(myData.getHistoryReview(),UserExamineStatus.class);
            Collections.sort(lists, new Comparator<UserExamineStatus>() {
                @Override
                public int compare(UserExamineStatus o1, UserExamineStatus o2) {
                    // 按照time字段进行升序排序
                    return -Long.compare(o1.getReviewTime().getTime(), o2.getReviewTime().getTime());
                }
            });
        }
        model.addAttribute("historyReviews",  lists   );
        model.addAttribute("applyForms", !StringUtil.isNotEmpty(myData.getApplyForm()) ?new String[0]:myData.getApplyForm().split(",")  );
        return "jzweb/signUp/infor";
    }
}
